/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-08-26 11:08:04
 * @LastEditTime: 2021-08-26 13:49:02
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */
#include "ft_assert.h"
#include "ar803x_phy.h"
#include "phy_common.h"

/**
 * @name: GmacAr803xReadReg
 * @msg: read Debug register for Ar803x PHY
 * @return {*}
 * @param {GmacCtrl} *pCtrl
 * @param {u16} reg, register offset
 * @param {u32} *pRegData, output data
 */
static u32 GmacAr803xReadReg(GmacCtrl *pCtrl, u16 reg, u32 *pRegData)
{
    FT_ASSERTZERONUM(pCtrl && pRegData);
    u32 ret = GMAC_SUCCESS;

    ret = GmacPhyWriteReg(pCtrl, GMAC_AR803X_DEBUG_ADDR, reg);
    if (GMAC_SUCCESS != ret)
        return ret;

    ret = GmacPhyReadReg(pCtrl, GMAC_AR803X_DEBUG_DATA, pRegData);
    return ret;
}

/**
 * @name: GmacAr803xMaskReg
 * @msg: set Debug register for Ar803x PHY
 * @return {*}
 * @param {GmacCtrl} *pCtrl
 * @param {u16} reg, register offset
 * @param {u32} clear, bits to clear
 * @param {u32} set, bits to set
 */
static u32 GmacAr803xMaskReg(GmacCtrl *pCtrl, u16 reg, u32 clear, u32 set)
{
    FT_ASSERTZERONUM(pCtrl);
    u32 ret = GMAC_SUCCESS;
    u32 val = 0;

    ret = GmacAr803xReadReg(pCtrl, reg, &val);
    if (GMAC_SUCCESS != ret)
        return ret;

    val &= ~clear;
    val |= set;

    ret = GmacPhyWriteReg(pCtrl, GMAC_AR803X_DEBUG_DATA, val);
    return ret;
}

/**
 * @name: GmacAr803xDisableHibernate
 * @msg: Disable power hibernate
 * @return {*}
 * @param {GmacCtrl} *pCtrl
 */
u32 GmacAr803xDisableHibernate(GmacCtrl *pCtrl)
{
    FT_ASSERTZERONUM(pCtrl);
    u32 ret = GMAC_SUCCESS;
    u32 regVal = 0;

    ret = GmacAr803xReadReg(pCtrl, GMAC_AR803X_DEBUG_HIB_CTRL_REG, &regVal);
    if (GMAC_SUCCESS != ret)
        return ret;

    regVal &= ~GMAC_AR803X_PS_HIB_EN;
    ret = GmacPhyWriteReg(pCtrl, GMAC_AR803X_DEBUG_DATA, regVal);

    regVal = 0;
    GmacAr803xReadReg(pCtrl, GMAC_AR803X_DEBUG_HIB_CTRL_REG, &regVal);
    GMAC_INFO("debug reg: 0x%lx, ret: 0x%lx", regVal, ret);

    return ret;
}